package com.haha.keymap;

import java.util.ArrayList;
import java.util.List;

public class KeyboardHook {

    private List<KeyboardListener> listeners = new ArrayList<>();

    public void addKeyListener(KeyboardListener listener) {
        listeners.add(listener);
    }

    public void start() {
        Thread hookThread = new Thread(() -> {
            while (true) {
                try {
                    // 模拟全局键盘监听 - 实际应用中应使用JNativeHook或JIntellitype等
                    Thread.sleep(50);
                    // 真实的全局键盘钩子实现会在此捕获系统级键盘事件
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    break;
                }
            }
        });
        hookThread.setDaemon(true);
        hookThread.start();
    }

    // 实际应用中会在这里触发键盘事件
    public void simulateKeyPress(int keyCode) {
        for (KeyboardListener listener : listeners) {
            listener.onKeyPressed(keyCode);
        }
    }

}
